home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / oleo-1_4.lha / oleo-1.4 / list.c < prev    next >
C/C++ Source or Header  |  1993-01-23  |  4KB  |  191 lines

  1. /*    Copyright (C) 1990, 1992, 1993 Free Software Foundation, Inc.
  2.  
  3. This file is part of Oleo, the GNU Spreadsheet.
  4.  
  5. Oleo is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. Oleo is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with Oleo; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "funcdef.h"
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. #include "sysdef.h"
  23. #include "global.h"
  24. #include "cell.h"
  25. #include "io-generic.h"
  26. #include "io-abstract.h"
  27. #include "regions.h"
  28. #include "io-utils.h"
  29. #include "cmd.h"
  30.  
  31.  
  32.  
  33.  
  34.  
  35. static char sl_sep = '\t';
  36.  
  37.  
  38.  
  39. /* This file reads/writes files containing values in separated lists,
  40.    sl_sep is the separating character.  This isn't really a save-file
  41.    format, but it is useful for reading and writing tables written by other
  42.    programs.
  43.  
  44.    Note that this format loses *most* of the information about the cells,
  45.    including formuale, formats, column widths, etc
  46.  */
  47. void
  48. list_read_file (fp, ismerge)
  49.      FILE *fp;
  50.      int ismerge;
  51. {
  52.   char cbuf[1024];
  53.   CELLREF row, col;
  54.   char *bptr;
  55.   char *eptr;
  56.   char *ptr;
  57.   char tchar;
  58.   int endit;
  59.   unsigned lineno;
  60.   int string;
  61.  
  62.   lineno = 0;
  63.   if (!ismerge)
  64.     clear_spreadsheet ();
  65.   row = curow;
  66.   col = cucol;
  67.  
  68.   while (fgets (&cbuf[1], sizeof (cbuf) - 3, fp))
  69.     {
  70.       lineno++;
  71.       if (lineno % 50 == 0)
  72.     io_info_msg ("Line %d", lineno);
  73.       endit = 0;
  74.       for (bptr = &cbuf[1];; bptr = eptr + 1)
  75.     {
  76.       eptr = (char *)index (bptr, sl_sep);
  77.       if (!eptr)
  78.         {
  79.           eptr = (char *)index (bptr, '\n');
  80.           endit++;
  81.         }
  82.       string = 0;
  83.       for (ptr = bptr; ptr != eptr; ptr++)
  84.         if (!isdigit (*ptr) && *ptr != '.' && *ptr != 'e' && *ptr != 'E')
  85.           {
  86.         string++;
  87.         break;
  88.           }
  89.       if (string)
  90.         {
  91.           bptr[-1] = '"';
  92.           eptr[0] = '"';
  93.           tchar = eptr[1];
  94.           eptr[1] = '\0';
  95.           new_value (row, col, &bptr[-1]);
  96.           eptr[1] = tchar;
  97.         }
  98.       else
  99.         {
  100.           eptr[0] = '\0';
  101.           new_value (row, col, bptr);
  102.         }
  103.       if (endit)
  104.         break;
  105.       col++;
  106.     }
  107.       row++;
  108.       col = cucol;
  109.     }
  110. }
  111.  
  112. void
  113. list_write_file (fp, rng)
  114.      FILE *fp;
  115.      struct rng *rng;
  116. {
  117.   CELLREF row, col;
  118.   int repressed;
  119.   CELL *cp;
  120.  
  121.   if (!rng)
  122.     rng = &all_rng;
  123.   for (row = rng->lr;; row++)
  124.     {
  125.       repressed = 0;
  126.       for (col = rng->lc;; col++)
  127.     {
  128.       if ((cp = find_cell (row, col)) && GET_TYP (cp))
  129.         {
  130.           while (repressed > 0)
  131.         {
  132.           putc (sl_sep, fp);
  133.           --repressed;
  134.         }
  135.           repressed = 1;
  136.           switch (GET_TYP (cp))
  137.         {
  138.         case TYP_FLT:
  139.           fputs (flt_to_str (cp->cell_flt), fp);
  140.           break;
  141.         case TYP_INT:
  142.           fprintf (fp, "%ld", cp->cell_int);
  143.           break;
  144.         case TYP_STR:
  145.           fputs (cp->cell_str, fp);
  146.           break;
  147.         case TYP_BOL:
  148.           fputs (bname[cp->cell_bol], fp);
  149.           break;
  150.         case TYP_ERR:
  151.           fputs (ename[cp->cell_err], fp);
  152.           break;
  153. #ifdef TEST
  154.         default:
  155.           panic ("Unknown type %d in write_sl_file()", GET_TYP (cp));
  156.           break;
  157. #endif
  158.         }
  159.         }
  160.       else
  161.         repressed++;
  162.       if (col == rng->hc)
  163.         break;
  164.     }
  165.       putc ('\n', fp);
  166.       if (row == rng->hr)
  167.     break;
  168.     }
  169. }
  170.  
  171. int
  172. list_set_options (set_opt, option)
  173.      int set_opt;
  174.      char *option;
  175. {
  176.   if (set_opt && !strincmp (option, "list ", 5))
  177.     {
  178.       option += 5;
  179.       sl_sep = string_to_char (&option);
  180.       return 0;
  181.     }
  182.   return -1;
  183. }
  184.  
  185. void
  186. list_show_options ()
  187. {
  188.   io_text_line ("File format: list    (character separated list of cell values)");
  189.   io_text_line ("Save file element separator: %s", char_to_string (sl_sep));
  190. }
  191.